package com.todo.user.infrastructure.repository.impl;

import com.alibaba.fastjson.JSONObject;
import com.todo.common.common.entity.response.page.PageResult;
import com.todo.common.common.enums.BizErrorCodeEnum;
import com.todo.common.common.exception.BizException;
import com.todo.common.utils.MathUtils;
import com.todo.user.infrastructure.entity.condition.UserAuthRecordCondition;
import com.todo.user.infrastructure.entity.po.UserAuthRecordPO;
import com.todo.user.infrastructure.mapper.UserAuthRecordMapper;
import com.todo.user.infrastructure.repository.UserAuthRecordRepository;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * (UserAuthRecord)表服务实现类
 *
 * @author zhangwenxiang
 * @since 2024-03-04 21:10:04
 */
@Slf4j
@Repository("userAuthRecordRepository")
public class UserAuthRecordRepositoryImpl implements UserAuthRecordRepository {
    @Resource
    private UserAuthRecordMapper userAuthRecordMapper;


    @Override
    public Boolean insertUserAuthRecordPO(UserAuthRecordPO userAuthRecordPO) {
        if (userAuthRecordPO == null) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        if (!baseFieldCheck(userAuthRecordPO)) {
            return false;
        }
        return userAuthRecordMapper.insertUserAuthRecordPO(userAuthRecordPO) > 0;
    }

    @Override
    public Boolean updateUserAuthRecordPOById(UserAuthRecordPO userAuthRecordPO) {
        if (userAuthRecordPO == null || userAuthRecordPO.getId() == null) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        if (!baseFieldCheck(userAuthRecordPO)) {
            return false;
        }
        return userAuthRecordMapper.updateUserAuthRecordPOById(userAuthRecordPO) > 0;
    }


    @Override
    public Boolean removeUserAuthRecordPOById(Long id) {
        if (id == null || id < 0) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        return userAuthRecordMapper.removeUserAuthRecordPOById(id) > 0;
    }

    @Override
    public UserAuthRecordPO queryUserAuthRecordPOById(Long id) {
        if (id == null || id < 0) {
            return null;
        }
        return userAuthRecordMapper.queryUserAuthRecordPOById(id);
    }

    @Override
    public PageResult<UserAuthRecordPO> queryUserAuthRecordPOByCondition(UserAuthRecordCondition userAuthRecordCondition) {
        PageResult<UserAuthRecordPO> pageResult = new PageResult<>();
        if (userAuthRecordCondition == null) {
            throw new BizException(BizErrorCodeEnum.DATA_NOT_EXIST);
        }
        pageResult.setPageNum(userAuthRecordCondition.getPageIndex());
        pageResult.setPageSize(userAuthRecordCondition.getPageSize());
        long count = queryCountByCondition(userAuthRecordCondition);
        if (count <= 0) {
            return pageResult;
        }
        pageResult.setPageCount((int) MathUtils.divide(count, userAuthRecordCondition.getPageSize()));
        pageResult.setTotal(count);
        List<UserAuthRecordPO> userAuthRecordPOList = userAuthRecordMapper.queryUserAuthRecordByCondition(userAuthRecordCondition);
        if (CollectionUtils.isEmpty(userAuthRecordPOList)) {
            pageResult.setHasNext(false);
            return pageResult;
        }
        pageResult.setResults(userAuthRecordPOList);
        pageResult.setCurrentIndex(userAuthRecordPOList.get(0).getId());
        pageResult.setHasNext(userAuthRecordPOList.size() >= userAuthRecordCondition.getPageSize());
        return pageResult;
    }

    @Override
    public Long queryCountByCondition(UserAuthRecordCondition userAuthRecordCondition) {
        if (userAuthRecordCondition == null || userAuthRecordCondition.getPageSize() <= 0) {
            throw new BizException(BizErrorCodeEnum.DATA_NOT_EXIST);
        }
        return userAuthRecordMapper.queryCountByCondition(userAuthRecordCondition);
    }


    private boolean baseFieldCheck(UserAuthRecordPO userAuthRecordPO) {
        if (userAuthRecordPO == null) {
            return false;
        }
        if (userAuthRecordPO.getUserId() == null || userAuthRecordPO.getUnionId() == null || userAuthRecordPO.getOpenId() == null) {
            log.warn("UserAuthRecordRepository#baseFieldCheck 核心字段校验失败，操作失败 userAuthRecordPO：【{}】", JSONObject.toJSONString(userAuthRecordPO));
            return false;
        }
        if (userAuthRecordPO.getPlatformKey() == null || userAuthRecordPO.getPlatformChannelKey() == null) {
            log.warn("UserAuthRecordRepository#baseFieldCheck 基础字段校验失败，操作失败 userAuthRecordPO：【{}】", JSONObject.toJSONString(userAuthRecordPO));
            return false;
        }
        return true;
    }
}
